package com.xjmall.controller.background;

import com.xjmall.common.Const;
import com.xjmall.common.ServerResult;
import com.xjmall.exception.AuthorityException;
import com.xjmall.exception.NotLoginException;
import com.xjmall.pojo.Category;
import com.xjmall.pojo.User;
import com.xjmall.service.ICategoryService;
import com.xjmall.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 后台商品分类Controller
 * Created by An on 2017/5/7.
 */
@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {

    @Autowired
    private ICategoryService iCategoryService;
    @Autowired
    private IFileService iFileService;

    /**
     * 添加商品分类
     * @param session 当前会话
     * @param category 待添加商品分类信息
     * @return ServerResult
     */
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult addCategory(HttpSession session, Category category) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.addCategory(category);
    }

    /**
     * 修改商品分类信息
     * @param session 当前会话
     * @param id 分类id
     * @param name 分类名称
     * @param url 分类图片url
     * @return ServerResult
     */
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult updateCategory(HttpSession session, Integer id, String name, String url) {
        // 获取当前登录用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.updateCategory(id, name, url);
    }

    /**
     * 根据分类id查询分类信息
     * @param session 当前会话
     * @param id 分类id
     * @return ServerResult
     */
    @RequestMapping(value = "get.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResult getCategoryById(HttpSession session, Integer id) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.getCategoryById(id);
    }

    /**
     * 根据分类的父id查询子分类列表
     * @param session 当前会话
     * @param parentId 分类父id
     * @return ServerResult
     */
    @RequestMapping(value = "get_child_categories.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult getChildrenParallelCategory(HttpSession session, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.getChildrenParallelCategory(parentId);
    }

    /**
     * 根据分类id批量删除分类信息
     * @param session 当前会话
     * @param delIds 分类id
     * @return ServerResult
     */
    @RequestMapping(value = "delete.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult deleteCategory(HttpSession session, String delIds) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.deleteCategoryByIds(delIds);
    }

    /**
     * 根据分类id修改分类状态
     * @param session 当前会话
     * @param id 分类id
     * @param status 分类状态
     * @return ServerResult
     */
    @RequestMapping(value = "update_status.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult updateCategoryStatus(HttpSession session, Integer id, Integer status) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iCategoryService.updateCategoryStatusById(id, status);
    }

//    @RequestMapping("get_deep_category.do")
//    @ResponseBody
//    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
//        User user = (User) session.getAttribute(Const.CURRENT_USER);
//        // 校验用户是否登录
//        if (user == null) {
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录后重试");
//        }
//        // 校验用户是否为管理员
//        if (iUserService.checkAdminRole(user).isSuccess()) {
//            // 执行到这，说明当前登录用户为管理员
//            return iCategoryService.selectCategoryAndChildrenById(categoryId);
//        } else {
//            return ServerResponse.createByErrorMessage("无操作权限，需要管理员权限");
//        }
//    }
}
